package thread.sisuo;

public class YouXuSuo {

    private static final Object tieLock = new Object();

    public void transferMoney(final Account fromAcct,final Account toAcct,final Account money){
            System.out.println("执行了外层的transferMoney方法");
        class Hleper{
            public void transfer(){
                System.out.println("执行了 Hleper中的transfer");
            }

        }
        int formHash = System.identityHashCode(fromAcct);
        int toHash = System.identityHashCode(toAcct);
        System.out.println("fromHash:"+formHash);
        System.out.println("toHash:"+toHash);
        if(formHash < toHash){
            synchronized (fromAcct){
                synchronized (toAcct){
                    new Hleper().transfer();
                }

            }

        }else if(formHash > toHash) {
            synchronized (toAcct){
                synchronized (fromAcct){
                    new Hleper().transfer();
                }
            }

        }
        else {
            synchronized (tieLock){
                synchronized (fromAcct){
                    synchronized (toAcct){
                        new Hleper().transfer();
                    }
                }
            }
        }
    }
}
